package com.qf.config;

import com.qf.constatns.RabbitMQConstatns;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {


    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(RabbitMQConstatns.ORDER_EXCHANGE, true, false, null);
    }

    @Bean
    public TopicExchange stockDlxExchange(){
        return new TopicExchange(RabbitMQConstatns.STOCK_DLX_EXCHANGE,true,false,null);
    }

    @Bean
    public Queue unLockStockQueue(){
        Map<String, Object> args = new HashMap();
        args.put("x-dead-letter-exchange", RabbitMQConstatns.STOCK_DLX_EXCHANGE); // 死信的交换机
        args.put("x-dead-letter-routing-key", "dlx.error"); // 死信路由键
        args.put("x-message-ttl",1000*10*12); // 队列中消息超过这个时间后就会变成死信
        return new Queue(RabbitMQConstatns.UNLOCK_STOCK_QUEUE,true,false,false,args);
    }

    @Bean
    public Queue stockDlxQueue(){
        return new Queue(RabbitMQConstatns.STOCK_DLX_QUEUE,true,false,false);
    }


    @Bean
    public Binding stockDlxQueueBind(){
        return BindingBuilder.bind(stockDlxQueue()).to(stockDlxExchange()).with("dlx.*");
    }

    @Bean
    public Binding unLockStockQueueBind(){
        return BindingBuilder.bind(unLockStockQueue()).to(orderExchange()).with("order.*");
    }








}
